package com.example.treehole.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.treehole.App;
import com.example.treehole.R;
import com.example.treehole.adapter.CourseAdapter;
import com.example.treehole.bean.Course;
import com.example.treehole.base.BaseBindingFragment;
import com.example.treehole.databinding.FragmentCourseScheduleBinding;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class CourseScheduleFragment extends BaseBindingFragment<FragmentCourseScheduleBinding> {

    private RecyclerView recyclerView;
    private CourseAdapter adapter;

    private void initCourseDataFromNetwork() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userid", App.user.id);
        HttpUtil.getList(Api.COURSE_GET, params, Course.class, new HttpUtil.OnNetListListener<Course>() {
            @Override
            public void success(List<Course> data, String message) {
                // 创建一个5天×8节的课程表矩阵，用一维列表存储
                List<Course> result = new ArrayList<>(45); // 预分配空间

                // 初始化所有位置为空白课程
                for (int day = 1; day <= 5; day++) {
                    for (int section = 1; section <= 8; section++) {
                        result.add(Course.makeEmptyCourse(day, section));
                    }
                }

                // 将实际课程数据填充到对应位置
                for (Course course : data) {
                    int day = course.getDayOfWeek();
                    int section = course.getClassNumber();
                    int position = 5 * section - (5 - day + 1);
                    course.setType(1);
                    result.set(position, course);
                }
                result.add(0,Course.makeWeekItem("周一"));
                result.add(1,Course.makeWeekItem("周二"));
                result.add(2,Course.makeWeekItem("周三"));
                result.add(3,Course.makeWeekItem("周四"));
                result.add(4,Course.makeWeekItem("周五"));
                // 设置RecyclerView布局管理器
                recyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 5));
                // 使用处理后的课程表数据设置适配器
                adapter = new CourseAdapter(result);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void error(String message) {
                // 显示错误提示
                Toast.makeText(requireContext(), "获取课程数据失败: " + message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void initData() {
        // 调用从网络获取课程数据的方法


    }

    @Override
    public void onResume() {
        super.onResume();
        initCourseDataFromNetwork();
    }

    @Override
    protected void initListener() {
        recyclerView = viewBinder.courseRecyclerView;

    }

}